#include <iostream>
#include <vector>
#include <string>
#include <unistd.h>
#include <pthread.h>

using namespace std;

const int threadnum = 5;

class ThreadData
{
public:
    ThreadData(string name, int x, int y):_threadname(name),_datax(x),_datay(y)
    {}

    string Tname()
    {
        return _threadname;
    }

    int result()
    {
        return _datax + _datay;
    }

private:
    string _threadname;
    int _datax;
    int _datay;
};

class Result
{
public:
    Result(string name, int sum):_threadname(name),_sum(sum)
    {}

    int result()
    {
        return _sum;
    }

    void Print()
    {
        cout<<_threadname<<" : "<<_sum<<endl; 
    }

private:
    string _threadname;
    int _sum;
};

void* handlertask(void* arg)
{
    ThreadData* td = static_cast<ThreadData*> (arg);
    
    Result* res = new Result(td->Tname(),td->result());
    delete td;
    sleep(2);
    return static_cast<void*>(res);
}

int main()
{
    vector<pthread_t> threads;
    for(int i = 0; i < threadnum; i++)
    {
        char threadname[64];
        snprintf(threadname,sizeof(threadname),"thread - %d",i+1);
        ThreadData* td = new ThreadData(threadname,66,88);
        pthread_t tid;
        pthread_create(&tid,nullptr,handlertask,td);
        threads.emplace_back(tid);
    }
    void* ret = nullptr; 
    vector<Result*> res;
    for(auto& tid:threads)
    {
        int n = pthread_join(tid,&ret);
        res.emplace_back(static_cast<Result*>(ret));
    }
   
    for(auto& re: res)
    {
        re->Print();
        delete re;
    }
  

    return 0;
}